package com.aw.baselib.base

import android.app.Dialog
import android.content.Context
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.aw.baselib.R

/**
 * Created by hzw
 */
class AwViewLoadingDialog(context: Context?) : AwBaseDialog(
    context!!
) {
    override val layoutId: Int
        get() = R.layout.view_dialog_loading

    override fun initLayoutParams(context: Context?, view: View?) {
        mDialog = Dialog(context!!, R.style.BaseDialog)
        mDialog!!.setContentView(
            view!!,
            LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT
            )
        )
        mDialog!!.setCanceledOnTouchOutside(false)
    }

    override fun show() {
        // main.xml中的ImageView
        val spaceshipImage = mDialog!!.findViewById<View>(R.id.img) as ImageView
        // 加载动画
        val hyperspaceJumpAnimation =
            AnimationUtils.loadAnimation(mContext, R.anim.loading_animation)
        // 使用ImageView显示动画
        spaceshipImage.startAnimation(hyperspaceJumpAnimation)
        mDialog!!.setOnDismissListener { spaceshipImage.clearAnimation() }
        showNormalScreen()
        mDialog!!.show()
    }

    fun showLoadingDialog(msg: String?) {
        val tipTextView = mDialog!!.findViewById<View>(R.id.text_view_message) as TextView // 提示文字
        tipTextView.visibility = View.VISIBLE
        if (null != msg) {
            tipTextView.text = msg
        }
        showNormalScreen()
        mDialog!!.show()
    }

    fun showMsg(msg: String?) {
        if (mDialog != null && mDialog!!.isShowing) {
            val tipTextView =
                mDialog!!.findViewById<View>(R.id.text_view_message) as TextView // 提示文字
            tipTextView.visibility = View.VISIBLE
            if (null != msg) {
                tipTextView.text = msg
            }
        }
    }

    /**
     * 不变暗
     */
    private fun showNormalScreen() {
        val window = mDialog!!.window
        val lp = window!!.attributes
        lp.dimAmount = 0f
        window.attributes = lp
    }
}